﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Floats : MonoBehaviour
{
	public List<BgObj> list;
	public float moveSpeed;

	void Start () {
		for (var i = 0; i < list.Count; i++) {
			var obj = list [i];
			if (i == 0) {
				obj.offPreCloud = Random.Range (10, 20);
			} else {
				var pre = list [i - 1];
				obj.offPreCloud = obj.transform.position.x - pre.transform.position.x;
			}
			obj.canFloat = true;
			if (obj.type == 1 || obj.type == 3) {
				obj.GetComponent<SpriteRenderer> ().sortingOrder = -2;
			} else if (obj.type == 2) {
				obj.GetComponent<SpriteRenderer> ().sortingOrder = -3;
			}
			obj.hight = obj.transform.position.y + 0.5f;
			obj.low = obj.transform.position.y - 0.5f;
		}
	}
	
	void Update () {
		for (var i = 0; i < list.Count; i++) {
			var obj = list [i];
			obj.transform.position = new Vector3 (obj.transform.position.x - moveSpeed*Time.deltaTime, obj.transform.position.y, obj.transform.position.z);
		}
		var obj1 = list [0];
		var camera = Camera.main.GetComponent<CameraMoved> ();
		if (camera.OutLeft (obj1.transform.position.x + obj1.width/2)) {
			var last = list [list.Count - 1];
			obj1.transform.position = new Vector3 (last.transform.position.x + obj1.offPreCloud, obj1.transform.position.y, obj1.transform.position.z);
			list.Remove (obj1);
			list.Add (obj1);
		}
	}
}
